// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: "Player 2 Object" Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.type			 : player.type
private alias object.groupID		 : player.groupID
private alias object.state			 : player.state
private alias object.visible		 : player.visible
private alias object.propertyValue	 : player.character
private alias object.priority		 : player.priority
private alias object.xpos			 : player.xpos
private alias object.ypos			 : player.ypos
private alias object.iypos			 : player.iypos
private alias object.lookPosY		 : player.lookPosY
private alias object.xvel			 : player.xvel
private alias object.yvel			 : player.yvel
private alias object.speed			 : player.speed
private alias object.rotation		 : player.rotation
private alias object.angle			 : player.angle
private alias object.direction		 : player.direction
private alias object.gravity		 : player.gravity
private alias object.frame			 : player.frame
private alias object.animation		 : player.animation
private alias object.prevAnimation	 : player.prevAnimation
private alias object.animationSpeed	 : player.animationSpeed
private alias object.animationTimer	 : player.animationTimer
private alias object.drawOrder		 : player.drawOrder
private alias object.controlLock	 : player.controlLock
private alias object.controlMode	 : player.controlMode
private alias object.interaction	 : player.interaction
private alias object.collisionPlane	 : player.collisionPlane
private alias object.tileCollisions	 : player.tileCollisions
private alias object.outOfBounds	 : player.outOfBounds

private alias object.jumpPress 	: player.jumpPress
private alias object.jumpHold 	: player.jumpHold
private alias object.up		 	: player.up
private alias object.down	 	: player.down
private alias object.left	 	: player.left
private alias object.right	 	: player.right

// The majority of these are the same as the Player Object, though a couple - namely the last few - are different
private alias object.value1  : player.timer
private alias object.value3  : player.drownTimer
private alias object.value4  : player.drownLevel
private alias object.value7  : player.invincibleTimer
private alias object.value8  : player.blinkTimer
private alias object.value12 : player.tailFrame
private alias object.value13 : player.tailAnim
private alias object.value16 : player.isSidekick
private alias object.value18 : player.sortedDrawOrder
private alias object.value19 : player.badnikBonus
private alias object.value25 : player.gravityStrength
private alias object.value26 : player.flightVelocity
private alias object.value30 : player.jumpOffset
private alias object.value31 : player.rollingDeceleration
private alias object.value32 : player.jumpAbility
private alias object.value33 : player.actionSpindash
private alias object.value34 : player.collisionDisabled
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom
private alias object.value42 : player.prevGravity

// These following values are exclusive to the Player 2 Object
// (Neat trivia - did you know that in the initial versions of the game, Origins's Drop Dash overlapped with these values?
//  That's why Tails wouldn't respawn a lot of the time, since every time he jumped his Drop Dash timer,
//  which overlapped with his respawn timer, would also reset. Nowadays though, the Drop Dash is a global var instead)
private alias object.value43 : player.jumpInTimer
private alias object.value44 : player.stateInputP2
private alias object.value45 : player.autoJumpTimer
private alias object.value46 : player.targetLeaderPos.x
private alias object.value47 : player.targetLeaderPos.y

// Draw Order Aliases
private alias -1 : DRAWORDER_PLAYER

// player.character Aliases
private alias 1 : CHARACTER_TAILS


// ========================
// Function Declarations
// ========================

reserve function Player2_GetDelayedInput
reserve function Player2_Input_GamepadAssist
reserve function Player2_Input_AI_Follow
reserve function Player2_Input_AI_SpindashPt1
reserve function Player2_Input_AI_SpindashPt2
reserve function Player2_State_FlyToPlayer
reserve function Player2_HandleSidekickRespawn
reserve function Player2_ProcessUpdateP2
reserve function Player2_Input_None
reserve function Player2_Action_DblJumpTailsAI


// ========================
// Static Values
// ========================

public value Player2_stateUp 			= 0
public value Player2_stateDown 			= 0
public value Player2_stateLeft 			= 0
public value Player2_stateRight 		= 0
public value Player2_stateJumpPress 	= 0
public value Player2_stateJumpHold 		= 0
public value Player2_nextLeaderPosID 	= 0
public value Player2_lastLeaderPosID 	= 0


// ========================
// Tables
// ========================

public table Player2_leaderPosBufferX[16]
public table Player2_leaderPosBufferY[16]


// ========================
// Function Definitions
// ========================

public function Player2_Input_None
	temp0 = 0
end function


public function Player2_Action_DblJumpTailsAI
	CheckNotEqual(player.stateInputP2, Player2_Input_None)
	temp0 = checkResult
	CheckNotEqual(object.up, true)
	temp0 &= checkResult
	if temp0 == false
		CallFunction(Player_Action_DblJumpTails)
	end if
end function


public function Player2_GetDelayedInput
	if object.controlMode > CONTROLMODE_NONE
		Player2_stateUp <<= 1
		Player2_stateUp |= player[0].up
		Player2_stateUp &= 0xFFFF
		Player2_stateDown <<= 1
		Player2_stateDown |= player[0].down
		Player2_stateDown &= 0xFFFF
		Player2_stateLeft <<= 1
		Player2_stateLeft |= player[0].left
		Player2_stateLeft &= 0xFFFF
		Player2_stateRight <<= 1
		Player2_stateRight |= player[0].right
		Player2_stateRight &= 0xFFFF
		Player2_stateJumpPress <<= 1
		Player2_stateJumpPress |= player[0].jumpPress
		Player2_stateJumpPress &= 0xFFFF
		Player2_stateJumpHold <<= 1
		Player2_stateJumpHold |= player[0].jumpHold
		Player2_stateJumpHold &= 0xFFFF

		if player[0].state == Player_State_Carried
			Player2_stateDown <<= 15
			Player2_stateLeft <<= 15
			Player2_stateRight <<= 15
			Player2_stateJumpPress <<= 15
			Player2_stateJumpHold <<= 15
		end if

		temp0 = Player2_stateUp
		temp0 >>= 15
		player.up = temp0

		temp0 = Player2_stateDown
		temp0 >>= 15
		player.down = temp0

		temp0 = Player2_stateLeft
		temp0 >>= 15
		player.left = temp0

		temp0 = Player2_stateRight
		temp0 >>= 15
		player.right = temp0

		temp0 = Player2_stateJumpPress
		temp0 >>= 15
		player.jumpPress = temp0

		temp0 = Player2_stateJumpHold
		temp0 >>= 15
		player.jumpHold = temp0
	else
		Player2_stateUp = 0
		Player2_stateDown = 0
		Player2_stateLeft = 0
		Player2_stateRight = 0
		Player2_stateJumpPress = 0
		Player2_stateJumpHold = 0
	end if

	if player[0].state != Player_State_Death
		if player[0].type != TypeName[Death Event]
			SetTableValue(player[0].xpos, Player2_lastLeaderPosID, Player2_leaderPosBufferX)
			SetTableValue(player[0].ypos, Player2_lastLeaderPosID, Player2_leaderPosBufferY)
			Player2_lastLeaderPosID++
			Player2_lastLeaderPosID &= 15
			Player2_nextLeaderPosID++
			Player2_nextLeaderPosID &= 15
			CheckEqual(player[0].gravity, 1)
			temp0 = checkResult
			CheckEqual(player[0].prevGravity, 0)
			temp0 &= checkResult
			if temp0 == 0
				GetTableValue(player.targetLeaderPos.x, Player2_nextLeaderPosID, Player2_leaderPosBufferX)
				GetTableValue(player.targetLeaderPos.y, Player2_nextLeaderPosID, Player2_leaderPosBufferY)
			else
				player.targetLeaderPos.x = player[0].xpos
				player.targetLeaderPos.y = player[0].ypos
			end if
		else
			temp0 = Player2_lastLeaderPosID
			temp0--
			if temp0 < 0
				temp0 += 16
			end if

			GetTableValue(player.targetLeaderPos.x, temp0, Player2_leaderPosBufferX)
			GetTableValue(player.targetLeaderPos.y, temp0, Player2_leaderPosBufferY)
		end if
	else
		temp0 = Player2_lastLeaderPosID
		temp0--
		if temp0 < 0
			temp0 += 16
		end if

		GetTableValue(player.targetLeaderPos.x, temp0, Player2_leaderPosBufferX)
		GetTableValue(player.targetLeaderPos.y, temp0, Player2_leaderPosBufferY)
	end if
end function


public function Player2_Input_GamepadAssist
#platform: USE_ORIGINS
	IsInputSlotAssigned(2)
	if checkResult == false
		player.stateInputP2 = Player2_Input_AI_Follow
		AssignInputSlotToDevice(2, -1)
		return
	end if
	
	ProcessObjectControl()
	
	temp0 = player.up
	temp0 |= player.down
	temp0 |= player.left
	temp0 |= player.right
	temp0 |= player.jumpHold
	if temp0 == false
		player.autoJumpTimer++
		if player.autoJumpTimer >= 600
			player.stateInputP2 = Player2_Input_AI_Follow
			AssignInputSlotToDevice(2, -1)
		end if
	else
		player.autoJumpTimer = 0
	end if
#endplatform
end function


public function Player2_Input_AI_Follow
	CallFunction(Player2_GetDelayedInput)
	if player[0].type == TypeName[Player Object]
		temp0 = player.angle
		temp0 += 16
		temp0 &= 0xE0
		if temp0 == 0
			if player.left == true
				temp0 = player[0].xpos
				temp0 -= 0x80000
				if player.xpos < temp0
					if player.xvel <= 0
						player.left = false
					end if
				end if
			end if

			if player.right == true
				temp0 = player[0].xpos
				temp0 += 0x80000
				if player.xpos > temp0
					if player.yvel <= 0
						player.right = false
					end if
				end if
			end if
		end if

		if player[0].state != Player_State_Carried
			temp0 = player.targetLeaderPos.x
			temp1 = player[0].gravity
			temp1 |= player[0].prevGravity
			if temp1 == false
				if player[0].speed < 0x20000
					if player[0].speed > -0x20000
						if player[0].direction == FACING_RIGHT
							temp0 -= 0x200000
						else
							temp0 += 0x200000
						end if
					end if
				end if
			end if

			temp0 -= player.xpos
			if temp0 != 0
				if temp0 < 0
					if temp0 <= -0x300000
						player.right = false
						player.left = true
					end if

					if player.speed != 0
						if player.direction == FACING_LEFT
							Cos256(temp1, player.angle)
							temp1 *= 0xC0
							player.xpos -= temp1
						end if
					end if
				else
					if temp0 >= 0x300000
						player.left = false
						player.right = true
					end if

					if player.speed != 0
						if player.direction == FACING_RIGHT
							Cos256(temp1, player.angle)
							temp1 *= 0xC0
							player.xpos += temp1
						end if
					end if
				end if
			end if

			if player.animation == ANI_PUSHING
				player.autoJumpTimer++
				if player[0].direction == player.direction
					if player[0].animation == ANI_PUSHING
						player.autoJumpTimer = 0
					end if
				end if

				if player.autoJumpTimer >= 30
					if player.gravity == 0
						CallFunction(Player_Action_Jump)
					end if
					player.timer = 0
					player.autoJumpTimer = 0
				end if
			else
				temp0 = player.ypos
				temp0 -= player.targetLeaderPos.y
				if temp0 > 0x200000
					player.autoJumpTimer++
					if player.autoJumpTimer >= 64
						if player.gravity == 0
							CallFunction(Player_Action_Jump)
						end if
						player.timer = 0
						player.autoJumpTimer = 0
					end if
				else
					player.autoJumpTimer = 0
				end if
			end if

			if player.controlLock > 0
				if player.speed < 0x8000
					if player.speed > -0x8000
						player.stateInputP2 = Player2_Input_AI_SpindashPt1
					end if
				end if
			end if
		end if
	end if
	
#platform: USE_ORIGINS
	// Look for any input from controller 2
	temp0 = keyDown[2].up
	temp0 |= keyDown[2].down
	temp0 |= keyDown[2].left
	temp0 |= keyDown[2].right
	temp0 |= keyDown[2].buttonA
	temp0 |= keyDown[2].buttonB
	temp0 |= keyDown[2].buttonC
	
	if temp0 == true
		// Enable P2 controls
		player.autoJumpTimer = 0
		player.stateInputP2 = Player2_Input_GamepadAssist
	end if
#endplatform
end function


public function Player2_Input_AI_SpindashPt1
	player.up = false
	player.down = false
	player.left = false
	player.right = false
	player.jumpPress = false
	player.jumpHold = false
	if player.controlLock == 0
		if player.gravity == 0
			if player.speed < 0x4000
				if player.speed > -0x4000
					player.stateInputP2 = Player2_Input_AI_SpindashPt2
					player.autoJumpTimer = 1
					if player.animation != ANI_SPINDASH
						if player.xpos < player.targetLeaderPos.x
							player.direction = FACING_RIGHT
						else
							player.direction = FACING_LEFT
						end if

						player.down = true
					end if
				end if
			end if
		end if
	end if
end function


public function Player2_Input_AI_SpindashPt2
	if player.autoJumpTimer < 64
		player.down = true
		temp0 = player.autoJumpTimer
		temp0 &= 15
		if temp0 == 0
			player.jumpPress = true
		else
			player.jumpPress = false
		end if
		player.autoJumpTimer++
	else
		player.autoJumpTimer = 0
		player.down = false
		player.jumpPress = false
		player.stateInputP2 = Player2_Input_AI_Follow
	end if
end function


public function Player2_State_FlyToPlayer
	CallFunction(Player2_GetDelayedInput)
	if player.gravityStrength == 0x3800
		player.animation = ANI_FLYING
	else
		player.animation = ANI_SWIMMING
	end if

	temp0 = player.targetLeaderPos.x
	temp0 -= player.xpos
	temp1 = temp0
	temp0 >>= 4

	if player.xpos < player.targetLeaderPos.x
		player.direction = FACING_RIGHT
		if temp0 > 0xC0000
			temp0 = 0xC0000
		end if

		if player[0].xvel > 0
			temp0 += player[0].xvel
		end if

		temp0 += 0x10000
		if temp0 > temp1
			temp0 = temp1
			temp1 = 0
		end if
	else
		player.direction = FACING_LEFT
		if temp0 < -0xC0000
			temp0 = -0xC0000
		end if

		if player[0].xvel < 0
			temp0 += player[0].xvel
		end if

		temp0 -= 0x10000
		if temp0 < temp1
			temp0 = temp1
			temp1 = 0
		end if
	end if
	player.xpos += temp0

	if player.ypos < player.targetLeaderPos.y
		player.ypos += 0x10000
	end if
	
	if player.ypos > player.targetLeaderPos.y
		player.ypos -= 0x10000
	end if

	if player[0].type != TypeName[Death Event]
		if player[0].state != Player_State_Death
			if player[0].state != Player_State_Drown
				if player[0].state != Player_State_TubeRoll
					if temp1 == 0
						temp0 = player.targetLeaderPos.y
						temp0 -= player.ypos
						if temp0 < 0
							FlipSign(temp0)
						end if

						if temp0 < 0x20000
							player.state = Player_State_Air
							player.animation = ANI_JUMPING
							player.gravity = GRAVITY_AIR
							player.tileCollisions = true
							player.interaction = true
							player.controlMode = CONTROLMODE_P2
							player.controlLock = 0
							player.angle = 0
							player.stateInputP2 = Player2_Input_AI_Follow
							player.collisionPlane = player[0].collisionPlane
							Player2_stateUp = 0
							Player2_stateDown = 0
							Player2_stateLeft = 0
							Player2_stateRight = 0
							Player2_stateJumpPress = 0
							Player2_stateJumpHold = 0
						end if
					end if
				end if
			end if
		end if
	end if
end function


public function Player2_HandleSidekickRespawn
	if player[0].type == TypeName[Player Object]
		if player.outOfBounds == true
			player.jumpInTimer++
		else
			player.jumpInTimer = 0
		end if

		if player.jumpInTimer >= 240
			player.jumpInTimer = 0
			player.state = Player2_State_FlyToPlayer
			player.xpos = player[0].xpos
			player.ypos = screen.yoffset
			player.ypos -= 128
			player.ypos <<= 16
			player.xvel = 0
			player.yvel = 0
			player.speed = 0
			player.tileCollisions = false
			player.interaction = false
			player.controlMode = CONTROLMODE_P2
			player.sortedDrawOrder = 4
			player.drownTimer = 0
			player.drownLevel = 0
		end if
		
		CheckEqual(player[0].state, Player_State_Death)
		temp0 = checkResult
		CheckEqual(player[0].type, TypeName[Death Event])
		temp0 |= checkResult
		CheckNotEqual(player.state, Player_State_Hurt)
		temp0 &= checkResult
		CheckNotEqual(player.state, Player_State_Death)
		temp0 &= checkResult
		if temp0 == true
			player.jumpInTimer = 0
			player.state = Player2_State_FlyToPlayer
			player.xvel = 0
			player.yvel = 0
			player.speed = 0
			player.tileCollisions = false
			player.interaction = false
		end if
	end if
end function


public function Player2_ProcessUpdateP2
	if player.state == Player_State_Death
		player.stateInputP2 = Player2_Input_None
	end if
	
	if player.state == Player_State_Drown
		player.stateInputP2 = Player2_Input_None
	end if

	CallFunction(player.stateInputP2)

	if player.state != Player_State_Hurt
		if player.blinkTimer > 0
			player.blinkTimer--
			GetBit(temp0, player.blinkTimer, 2)
			if temp0 == true
				player.visible = false
			else
				player.visible = true
			end if
		end if
	end if

	if player.invincibleTimer > 0
		if player.state != Player_State_Hurt
			if player.invincibleTimer > 2000
				player.invincibleTimer = 120
				player.blinkTimer = 3
			end if
		end if

		player.invincibleTimer--
		if player.invincibleTimer == 0
			player.blinkTimer = 0
			player.visible = true
		end if
	end if

	if player.state != Player_State_LookUp
		if player.state != Player_State_Crouch
			if player.lookPosY > 0
				player.lookPosY -= 2
			end if

			if player.lookPosY < 0
				player.lookPosY += 2
			end if
		end if
	end if

	if player.state != Player_State_Fly
		if player.flightVelocity != 0
			StopSfx(SfxName[Flying])
			StopSfx(SfxName[Tired])
			player.flightVelocity = 0
		end if
	end if
end function


// ========================
// Events
// ========================

event ObjectUpdate
	CallFunction(Player2_ProcessUpdateP2)
	CallFunction(player.state)
	ProcessAnimation()
	CallFunction(TailsObject_ProcessTailSprite)

	if player.collisionDisabled == false
		temp0 = player.prevGravity
		player.prevGravity = player.gravity
		ProcessObjectMovement()
		player.prevGravity ^= 1
		CheckEqual(player.gravity, 0)
		player.prevGravity |= checkResult
		player.prevGravity ^= 1
		if temp0 == 1
			if player.prevGravity == 0
				player.badnikBonus = 0
				if player.animation == ANI_JUMPING
					if player.down == false
						if player.state != Player_State_Roll
							if player.state != Player_State_TubeRoll
								player.animation = ANI_WALKING
								player.iypos += player.jumpOffset
							end if
						end if
					end if
				end if
			end if
		end if
	else
		player.collisionDisabled = false
	end if

	CallFunction(Player2_HandleSidekickRespawn)
end event


event ObjectDraw
	if player.animation != player.prevAnimation
		player.prevAnimation = player.animation
		player.frame = 0
		player.animationTimer = 0
		player.animationSpeed = 0
	end if
	
	if player.tailAnim != player.animation
		if player.animation > 4
			player.tailFrame = 0
		end if

		if player.tailAnim > 4
			player.tailFrame = 0
		end if

		player.tailAnim = player.animation
	end if
	
	// Draw the Tails part of Tails
	switch player.tailAnim
	case 0 // ANI_STOPPED
		if player.frame == 0
			temp0 = player.tailFrame
			temp0 >>= 3
			if player.visible == true
				DrawSpriteFX(temp0, FX_FLIP, player.xpos, player.ypos)
			end if
		end if
		break
		
	case 1 // ANI_WAITING
	case 3 // ANI_LOOKINGUP
	case 4 // ANI_LOOKINGDOWN
	case 37 // ANI_CONTINUE_UP
		temp0 = player.tailFrame
		temp0 >>= 3
		if player.visible == true
			DrawSpriteFX(temp0, FX_FLIP, player.xpos, player.ypos)
		end if
		break
		
	case 7 // ANI_SKIDDING
	case 9 // ANI_SPINDASH
		temp0 = player.tailFrame
		temp0 >>= 2
		temp0 += 11
		if player.visible == true
			DrawSpriteFX(temp0, FX_FLIP, player.xpos, player.ypos)
		end if
		break
		
	case 10 // ANI_JUMPING
	case 43 // Unknown, this was probably "SSRoll" before it got moved to its own ani file
		temp0 = player.tailFrame
		temp0 >>= 2
		CheckEqual(player.xvel, 0)
		temp1 = checkResult
		CheckEqual(player.yvel, 0)
		temp1 &= checkResult
		temp2 = player.rotation
		if temp1 == 0
			ATan2(player.rotation, player.xvel, player.yvel)
			player.rotation += 16
			player.rotation &= 255
			player.rotation >>= 5
			switch player.rotation
			case 0
			case 8
				temp0 += 5
				player.rotation = 0x00
				break

			case 1
				temp0 += 8
				if player.direction == FACING_RIGHT
					player.rotation = 0x40
				else
					player.rotation = 0x00
				end if
				break

			case 2
				temp0 += 5
				player.rotation = 0x40
				break

			case 3
				temp0 += 8
				if player.direction == FACING_RIGHT
					player.rotation = 0x80
				else
					player.rotation = 0x40
				end if
				break

			case 4
				temp0 += 5
				player.rotation = 0x80
				break

			case 5
				temp0 += 8
				if player.direction == FACING_RIGHT
					player.rotation = 0xC0
				else
					player.rotation = 0x80
				end if
				break

			case 6
				temp0 += 5
				player.rotation = 0xC0
				break

			case 7
				temp0 += 8
				if player.direction == FACING_RIGHT
					player.rotation = 0x00
				else
					player.rotation = 0xC0
				end if
				break
			end switch

			if player.direction == FACING_LEFT
				player.rotation += 128
			end if
		else
			temp0 += 5
			player.rotation = 0
		end if

		player.rotation <<= 1
		if player.visible == true
			DrawSpriteFX(temp0, FX_ROTATE, player.xpos, player.ypos)
		end if
		player.rotation = temp2
		break
		
	case 17 // ANI_PUSHING
		temp0 = player.tailFrame
		temp0 /= 10
		temp0 += 11
		if player.visible == true
			DrawSpriteFX(temp0, FX_FLIP, player.xpos, player.ypos)
		end if
		break
		
	end switch
	
	// Draw Tails himself
	// (Separate tails are drawn in the process above)
	DrawObjectAnimation()
end event


event ObjectStartup
	Player2_nextLeaderPosID 	= 1
	Player2_lastLeaderPosID 	= 0
	Player2_stateUp 			= 0
	Player2_stateDown 		= 0
	Player2_stateLeft 		= 0
	Player2_stateRight 		= 0
	Player2_stateJumpPress 	= 0
	Player2_stateJumpHold 	= 0

	if stage.player2Enabled == true
		// Always make P2 start 16 pixels to the left of P1, regardless of if a P2 object is placed in the scene or not
		// (It should never be anyways, but MZ1 does a few way OOB for some reason...)
		ResetObjectEntity(1, TypeName[Player 2 Object], 1, player[0].xpos, player[0].ypos)
		player[1].xpos -= 0x100000
		
		// We're using the ani for Tails's main sprites, and the Sprite Frames for his Tails
		LoadSpriteSheet("Players/Tails1.gif")
		LoadAnimation("Tails.ani")
		
		SpriteFrame(-22, -8, 16, 24, 82, 199)
		SpriteFrame(-26, -8, 20, 24, 99, 199)
		SpriteFrame(-26, -8, 20, 24, 120, 199)
		SpriteFrame(-26, -8, 20, 24, 141, 199)
		SpriteFrame(-26, -8, 20, 24, 162, 199)
		SpriteFrame(-35, -8, 24, 16, 231, 166)
		SpriteFrame(-35, -8, 24, 16, 231, 183)
		SpriteFrame(-35, -8, 24, 16, 231, 200)
		SpriteFrame(-25, 9, 20, 16, 235, 217)
		SpriteFrame(-25, 9, 18, 16, 237, 234)
		SpriteFrame(-25, 9, 20, 16, 216, 234)
		SpriteFrame(-30, -6, 24, 16, 231, 166)
		SpriteFrame(-30, -6, 24, 16, 231, 183)
		SpriteFrame(-30, -6, 24, 16, 231, 200)
		
		player[1].groupID = GROUP_PLAYERS
		player[1].character = CHARACTER_TAILS
		player[1].state = Player_State_Air
		player[1].priority = PRIORITY_ACTIVE
#platform: USE_STANDALONE
		player[1].controlMode = CONTROLMODE_P1
#endplatform
#platform: USE_ORIGINS
		player[1].controlMode = CONTROLMODE_P2
#endplatform
		player[1].drawOrder = DRAWORDER_PLAYER
		player[1].sortedDrawOrder = 4
		currentPlayer = 1
		CallFunction(Player_UpdatePhysicsState)
		player[1].rollingDeceleration = 0x2000
		player[1].jumpOffset = -1
		player[1].isSidekick = true
		player[1].jumpAbility = Player2_Action_DblJumpTailsAI
		
		if options.spindash == true
			player[1].actionSpindash = Player_Action_Spindash
		else
			player[1].actionSpindash = Player_Action_Jump
		end if

		CheckCurrentStageFolder("Continue")
		if checkResult == true
			player[1].stateInputP2 = Player2_Input_None
		else
			player[1].stateInputP2 = Player2_Input_AI_Follow
		end if
		
		// Reset the position buffer table, fill it up with P1's starting position for now
		temp0 = 0
		while temp0 < 16
			SetTableValue(player[0].xpos, temp0, Player2_leaderPosBufferX)
			SetTableValue(player[0].ypos, temp0, Player2_leaderPosBufferY)
			temp0++
		loop
		
		// Set player hitboxes
		player[1].hitboxLeft 	= C_BOX
		player[1].hitboxRight 	= C_BOX
		player[1].hitboxTop 	= C_BOX
		player[1].hitboxBottom 	= C_BOX
		
#platform: USE_ORIGINS
		AssignInputSlotToDevice(2, -1)
#endplatform
	end if
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Players/Tails1.gif")
	SpriteFrame(-22, -8, 16, 24, 82, 199) // snip
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
